package com.zzty.servlet;

import com.zzty.common.ReadFile;
import com.zzty.dao.SelfExamDao;
import com.zzty.daoimpl.SelfExamDaoImpl;
import com.zzty.entity.*;
import com.zzty.service.SelfExamService;
import com.zzty.service.SelfExamTransactionService;
import com.zzty.serviceimpl.SelfExamServiceImpl;
import com.zzty.serviceimpl.SelfExamTransactionServiceImpl;
import org.codehaus.jackson.map.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "SelfExamTransactionServlet",urlPatterns = "/SelfExamTransactionServlet")
public class SelfExamTransactionServlet extends HttpServlet {
    // String name=(String)request.getSession().getAttribute("name");  //获取前台session；
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
//        if ("startExam".equals(method)) {
//            startExam(request, response);
//        }
        if ("getTable".equals(method)) {
            this.getTable(request, response);
        }
        if ("searchAnswer".equals(method)) {
            this.searchAnswer(request, response);
        }
        if ("createTable1".equals(method)) {
            this.createTable1(request, response);
        }
    }
//    protected void startExam(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
////        int userId=(int)request.getSession().getAttribute("id");
//        int userId=5;
//        System.out.println("servlet id is"+userId);
//        SelfExamService start = new SelfExamServiceImpl();
//        Question question=new Question();
//        question.setId(start.startSelfExam(userId));
//        SelfExamDao selfExamDao=new SelfExamDaoImpl();
//        question.setContent(selfExamDao.getQuestionById(question.getId()));
//        String json=null;
//        ObjectMapper mapper = new ObjectMapper();
//        if(question!=null){
//            json=mapper.writeValueAsString(question.getContent());
//        }
//        System.out.println(json);
//        PrintWriter out= response.getWriter();
//        out.write(json);
//        out.flush();
//        out.close();
//    }

//    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        String method = request.getParameter("method");
//        if ("searchSelfQuestion".equals(method)) {
//            searchSelfQuestion(request, response);
//        }
//
//    }

    //    protected void searchSelfQuestion(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        SelfExamService start = new SelfExamServiceImpl();
//        List<SelfQuestion> questions=null;
//        String json=null;
//        ObjectMapper mapper = new ObjectMapper();
//        if(start.historyQuestion()!=null){
//            questions=start.historyQuestion();
//            json = mapper.writeValueAsString(questions);
//        }
//        PrintWriter out= response.getWriter();
//
//        out.write(json);
//        out.flush();
//        out.close();
//    }
    protected void getTable(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        SelfExamTransactionService ses = new SelfExamTransactionServiceImpl();
        List<SelfExam> slefExams = null;
        String json = null;
        ObjectMapper mapper = new ObjectMapper();
        if (ses.getSelfExamToTable()!= null) {
            slefExams = ses.getSelfExamToTable();
            json = mapper.writeValueAsString(slefExams);
        }
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }

    protected void searchAnswer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer examId = Integer.valueOf(request.getParameter("examId"));
        User user =(User) request.getSession().getAttribute("user");
        Integer userId=user.getId();
        System.out.print("examId = "+examId+";"+"id = "+userId);
        SelfExamTransactionService ses = new SelfExamTransactionServiceImpl();
        List<AnswerPage> answer = null;
        String json = null;
        ObjectMapper mapper = new ObjectMapper();
        //String answerUrl="C:\\huangxinyi\\";
        if (ses.searchAnswerById(examId,userId)!=null){
            answer = ses.searchAnswerById(examId,userId);
            json = mapper.writeValueAsString(answer);
        }
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }

    protected void createTable1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SelfExamTransactionService sets = new SelfExamTransactionServiceImpl();
        List<SelfExamTransaction> selfExamTransaction = null;
        String json = null;
        ObjectMapper mapper = new ObjectMapper();
        if (sets.createTable()!= null) {
            selfExamTransaction = sets.createTable();
            json = mapper.writeValueAsString(selfExamTransaction);
        }
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
}
